package com.hrsystem.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hrsystem.entity.OrgFirst;
import com.hrsystem.entity.User;
import com.hrsystem.lang.Result;
import com.hrsystem.mapper.OrgFirstMapper;
import com.hrsystem.service.OrgFirstService;
import com.hrsystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;


@RestController
public class OrgFirstController {

    @Resource
    OrgFirstMapper orgFirstMapper;

    @Autowired
    OrgFirstService orgFirstService;

    @Transactional
    @GetMapping("/selectFirstOrg")
    public Result selectFirstOrgList(){
        QueryWrapper<OrgFirst> queryWrapper = new QueryWrapper<>();
        queryWrapper.select();
        List<OrgFirst> orgFirstOrgList = orgFirstMapper.selectList(queryWrapper);
        return Result.success(orgFirstOrgList);
    }

    @Transactional
    @PostMapping("/AddFirstOrg")
    public Result addFirstOrg(@RequestBody OrgFirst orgFirst) {
        List<OrgFirst> orgFirstOrgList=orgFirstService.list();
        for(OrgFirst orgFirstOrg:orgFirstOrgList){
            if(Objects.equals(orgFirstOrg.getForgName(), orgFirst.getForgName())){
                return Result.fail("该一级机构已存在");
            }
        }
        orgFirstService.saveOrUpdate(orgFirst);
        return Result.success(null);
    }


}
